package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
/**
 *
 * @author chopper
 * 同步员工信息
 *
 */
public class SyscHrmSubcompany implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(SyscHrmSubcompany.class);
	private String charset = "utf-8";
	private int timeout = 100000;
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		PropertiesUtil properties = new PropertiesUtil();
		def hrlurl = properties.readValue("Hrmurl");
		log.debug("Hrmurl:"+hrlurl);

		def hrlip = properties.readValue("Hrmip");
		log.debug("Hrmip:"+hrlip);

		def guid=Util.getGuid();
		log.debug("guid:"+guid);
		
		def xmlbody = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
								<SOAP-ENV:Body>
									<m:getHrmUserInfo xmlns:m="http://localhost/services/HrmService">
										<m:in0>"""+hrlip+"""</m:in0>
										<m:in1></m:in1>
										<m:in2></m:in2>
										<m:in3></m:in3>
										<m:in4></m:in4>
										<m:in5></m:in5>
									</m:getHrmUserInfo>
								</SOAP-ENV:Body>
							</SOAP-ENV:Envelope>""";
		//log.debug("调用员工信息接口发送报文信息："+xmlbody);
		//调用员工信息接口
		def xml=Util.callWebService(hrlurl,xmlbody,charset,'',timeout);
		xml = Util.Transform(xml); //去命令空间
		xml = new XmlParser().parseText(xml); //xml解析
		//log.debug("调用员工信息接口返回报文信息："+xml);
		def result ="";
		try{
			result =xml.Body.getHrmUserInfoResponse.out.UserBean;
			}catch (Exception e){
				result="";
				log.error("EM:",e);
			};
			log.debug("result："+result);
			log.debug("size"+result.size());
			if(result==null||(result!=null&&result.size()==0)){
				return "{\"code\":\"-1\",\"msg\":\"调用接口返回异常，请查看日志\"}";
			}
			
		result.each{
			try{
				//log.debug("Deal hrm_staff--" + "departmentid:"+it.departmentid.text()+" departmentname:"+it.departmentname.text()+" dsporder:"+it.dsporder.text()+" joblevel:"+it.joblevel.text()+" jobtitle:"+it.jobtitle.text()+ //"assistantid:"+it.assistantid.text()+" createdate:"+it.createdate.text()+" lastChangdate:"+it.lastChangdate.text()+" lastname:"+it.lastname.text()+" locationid:"+it.locationid.text()+ "loginid:"+it.loginid.text()+" //maritalstatus:"+it.maritalstatus.text()+" mobile:"+it.mobile.text()+" password:"+it.password.text()+" seclevel:"+it.seclevel.text()+ "sex:"+it.sex.text()+" status:"+it.status.text()+" subcompanyid1:"+it.subcompanyid1.text()+" //subcompanyname:"+it.subcompanyname.text()+" telephone:"+it.telephone.text()+" userid:"+it.userid.text()+" workcode:"+it.workcode.text()+" mobilecall:"+it.mobilecall.text());
				
				//增加不存在的员工
				eliteBean.update("INSERT INTO hrm_staff (sta_guid,departmentid,departmentname,dsporder,joblevel,jobtitle,assistantid,createdate,lastChangdate,lastname,locationid,loginid,maritalstatus,mobile,password,seclevel,sex,status,subcompanyid1,subcompanyname,telephone,userid,workcode,mobilecall,hrm_id,modifydate) select 	newid(),t.departmentid,t.departmentname,t.dsporder,t.joblevel,t.jobtitle,t.assistantid,t.createdate,t.lastChangdate,t.lastname,t.locationid,t.loginid,t.maritalstatus,t.mobile,t.password,t.seclevel,t.sex,t.status,t.subcompanyid1,t.subcompanyname,t.telephone,t.userid,t.workcode,t.mobilecall,'"+guid+"',getdate() from (select	CASE WHEN '"+it.departmentid.text()+"' = '' THEN	''ELSE	'BM' + '"+it.departmentid.text()+"'END departmentid,'"+it.departmentname.text()+"' departmentname,'"+it.dsporder.text()+"' dsporder,'"+it.joblevel.text()+"' joblevel,'"+it.jobtitle.text()+"' jobtitle,'"+it.assistantid.text()+"' assistantid,'"+it.createdate.text()+"' createdate,'"+it.lastChangdate.text()+"' lastChangdate,'"+it.lastname.text()+"' lastname,'"+it.locationid.text()+"' locationid,'"+it.loginid.text()+"' loginid,'"+it.maritalstatus.text()+"' maritalstatus,'"+it.mobile.text()+"' mobile,'"+it.password.text()+"' password,'"+it.seclevel.text()+"' seclevel,'"+it.sex.text()+"' sex,'"+it.status.text()+"' status,'"+it.subcompanyid1.text()+"' subcompanyid1,'"+it.subcompanyname.text()+"' subcompanyname,'"+it.telephone.text()+"' telephone,'"+it.userid.text()+"' userid,'"+it.workcode.text()+"' workcode,'"+it.mobilecall.text()+"' mobilecall) t LEFT JOIN hrm_staff hs on hs.userid=t.userid AND hs.userid = '"+it.userid.text()+"' WHERE hs.userid IS NULL","dataSource");
				
				//更新存在的员工
				eliteBean.update("update hrm_staff set departmentid =CASE WHEN '"+it.departmentid.text()+"' = '' THEN '' ELSE 'BM' + '"+it.departmentid.text()+"' END,departmentname ='"+it.departmentname.text()+"',dsporder ='"+it.dsporder.text()+"',joblevel ='"+it.joblevel.text()+"',jobtitle ='"+it.jobtitle.text()+"',assistantid ='"+it.assistantid.text()+"',createdate ='"+it.createdate.text()+"',lastChangdate ='"+it.lastChangdate.text()+"',lastname ='"+it.lastname.text()+"',locationid ='"+it.locationid.text()+"',loginid ='"+it.loginid.text()+"',maritalstatus ='"+it.maritalstatus.text()+"',mobile ='"+it.mobile.text()+"',password ='"+it.password.text()+"',seclevel ='"+it.seclevel.text()+"',sex ='"+it.sex.text()+"',status ='"+it.status.text()+"',subcompanyid1 ='"+it.subcompanyid1.text()+"',subcompanyname ='"+it.subcompanyname.text()+"',telephone ='"+it.telephone.text()+"',workcode ='"+it.workcode.text()+"',mobilecall ='"+it.mobilecall.text()+"',hrm_id='"+guid+"',modifydate = getdate() where userid ='"+it.userid.text()+"'","dataSource");
			}catch (Exception e){
				log.error("EM:",e);
			};
		};
		eliteBean.update("delete from hrm_staff where hrm_id!='"+guid+"'","dataSource"); //删除员工历史数据,保留本次同步信息
		
		def sqltype = eliteBean.chaxun_sql("select count(*) from hrm_staff where hrm_id='"+guid+"'","dataSource"); //判断员工数据是否正常插入
		if(sqltype==0) {
				return "{\"code\":\"-2\",\"msg\":\"调用员工信息接口返回为空，请查看日志\"}";
			};
		
		//删除接口同步，并且CRM无修改过的数据
		eliteBean.update("delete from org_staffinfo where datafrom=1 and modifytype=0","dataSource");
		
		//写入新增数据
		eliteBean.update("insert into org_staffinfo(userid,lastname,departmentid,jobtitle,createdate,lastchangdate,mobile,telephone,mobilecall,sex,status,dsporder,datafrom,modifydate,modifytype) select hs.userid,hs.lastname,hs.departmentid,hs.jobtitle,hs.createdate,hs.lastchangdate,hs.mobile,hs.telephone,hs.mobilecall,hs.sex,hs.status,hs.dsporder,1,getdate(),0 from hrm_staff hs left join org_staffinfo os on hs.userid=os.userid where os.userid is null ","dataSource");
		
		//更新接同步并且CRM已经修改过的数据
		eliteBean.update("update org_staffinfo set lastname=hs.lastname,jobtitle=hs.jobtitle,lastchangdate=hs.lastchangdate,mobile=hs.mobile,telephone=hs.telephone,mobilecall=hs.mobilecall,sex=hs.sex,status=hs.status,dsporder=hs.dsporder from org_staffinfo os,hrm_staff hs where hs.userid=os.userid and os.datafrom=1 and os.modifytype=1","dataSource");
		return "{\"code\":\"1\",\"msg\":\"执行同步更新成功\"}";
	}
}

